package org.edu.modules.scheduleCommon.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.edu.modules.scheduleCommon.constant.RoleConstant;
import org.edu.modules.scheduleCommon.entity.VeBaseBanji;
import org.edu.modules.scheduleCommon.entity.VeBaseTeacher;
import org.edu.modules.scheduleCommon.mapper.VeBaseAppUserMapper;
import org.edu.modules.scheduleCommon.mapper.VeBaseBanjiMapper;
import org.edu.modules.scheduleCommon.mapper.VeBaseTeacherMapper;
import org.edu.modules.scheduleCommon.service.VeBaseTeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class VeBaseTeacherServiceImpl extends ServiceImpl<VeBaseTeacherMapper, VeBaseTeacher> implements VeBaseTeacherService {

    @Autowired
    private VeBaseAppUserMapper appUserMapper;

    @Autowired
    private VeBaseTeacherMapper veBaseTeacherMapper;

    @Autowired
    private VeBaseBanjiMapper veBaseBanjiMapper;

    @Override
    public VeBaseTeacher getByUserId(String userId) {
        return veBaseTeacherMapper.getByUserId(userId);
    }

    @Override
    public int importExcel(List<Map<String, Object>> list) {

        return 0;
    }

    @Override
    public List<Integer> getBjIdsListBySysUserId(Integer semId, String userId) {
        //获取角色
        List<Map<String, Object>> roleList = appUserMapper.getBaseSysRoleList(userId);
        VeBaseTeacher baseTeacher = veBaseTeacherMapper.getByUserId(userId);
        List<Integer> bjIdsList = new ArrayList<>();
        if (baseTeacher != null) {
            for (Map<String, Object> role : roleList) {
                /*if (role.get("code").equals(RoleConstant.SYS_ROLE_BZR)) {
                    //是班主任
                    //查班级
                    List<VeBaseBanji> veBaseBanjiList = veBaseBanjiMapper.listByTeacherId(baseTeacher.getUserId());
                    if (veBaseBanjiList != null && veBaseBanjiList.size() > 0) {
                        bjIdsList = veBaseBanjiList.stream().map(veBaseBanji -> veBaseBanji.getId()).collect(Collectors.toList());
                        return bjIdsList;
                    } else {
                        //如果为空的话，学生id为0，即查不到数据
                        bjIdsList.add(0);
                        return bjIdsList;
                    }
                } */
                if (role.get("code").equals(RoleConstant.SYS_ROLE_TEACHER)) {
                    //是教师
                    //查教学班级（有教学任务的班级）
                    List<VeBaseBanji> veBaseBanjiList = veBaseBanjiMapper.getBanJiListBySemIdAndTeacherId(semId, baseTeacher.getUserId());
                    if (veBaseBanjiList != null && veBaseBanjiList.size() > 0) {
                        bjIdsList = veBaseBanjiList.stream().map(veBaseBanji -> veBaseBanji.getId()).collect(Collectors.toList());
                        return bjIdsList;
                    } else {
                        //如果为空的话，班级id为0，即查不到数据
                        bjIdsList.add(0);
                        return bjIdsList;
                    }
                }
            }
            if (bjIdsList.size() == 0) {
                //如果为空的话，班级id为0，即查不到数据
                bjIdsList.add(0);
                return bjIdsList;
            }
        }
        return bjIdsList;
    }
}
